package com.unitymarvel.harmonyos.projects.findme.slice;

import com.unitymarvel.harmonyos.projects.findme.MainAbility;
import com.unitymarvel.harmonyos.projects.findme.ResourceTable;
import com.unitymarvel.harmonyos.projects.findme.common.DeviceProvider;
import com.unitymarvel.harmonyos.projects.findme.common.Tools;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.distributedschedule.interwork.DeviceInfo;
import ohos.distributedschedule.interwork.DeviceManager;
import ohos.distributedschedule.interwork.IDeviceStateCallback;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;

import java.util.List;
///////////////////////////////////////////////////
// 项目的详细实现过程：https://ke.qq.com/course/3677276?tuin=a22a65ce
// 关注微信公众号和B站，获取更多鸿蒙学习资源
// 微信公众号：极客起源
// B站：https://space.bilibili.com/477001733
//
// 作者:李宁（蒙娜丽宁）
//
// 版本: 1.0
// Copyright (C), 2001-2022, 李宁
// 程序被版权法保护
//
///////////////////////////////////////////////////
public class DeviceListAbilitySlice extends AbilitySlice {
    private Text textEmpty;
    private DeviceProvider deviceProvider;
    // 初始化组件
    private void initComponents() {
        ListContainer listContainer = (ListContainer)findComponentById(ResourceTable.Id_container_list);
        textEmpty = (Text)findComponentById(ResourceTable.Id_text_empty);
        deviceProvider = new DeviceProvider(this, getDeviceList());
        listContainer.setItemProvider(deviceProvider);
        listContainer.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
               // System.out.println("clicked");
                //Tools.showTip(DeviceListAbilitySlice.this, Tools.getLocalIP(DeviceListAbilitySlice.this));
                DeviceInfo deviceInfo = (DeviceInfo)deviceProvider.getItem(i);
                try {
                    continueAbility(deviceInfo.getDeviceId());
                }catch (Exception e) {

                }

            }
        });

       updateComponent();

    }
    private void updateComponent() {
        textEmpty.setVisibility(getDeviceList().size() == 0?Component.VISIBLE:Component.HIDE);
    }
    private List<DeviceInfo> getDeviceList() {
        return DeviceManager.getDeviceList(DeviceInfo.FLAG_GET_ALL_DEVICE);
    }
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_device_list);
        initComponents();
    }
    private EventHandler handler = new EventHandler(EventRunner.current()) {
        @Override
        protected void processEvent(InnerEvent event) {

            deviceProvider.updateData(getDeviceList());
            updateComponent();
            registerCallback();

        }
    };
    // 用于实现实时监控
    private IDeviceStateCallback iDeviceStateCallback = new IDeviceStateCallback() {
        @Override
        public void onDeviceOffline(String s, int i) {
            handler.sendEvent(100);
        }

        @Override
        public void onDeviceOnline(String s, int i) {
            handler.sendEvent(100);
        }
    };
    private void registerCallback() {
        DeviceManager.registerDeviceStateCallback(iDeviceStateCallback);
    }
    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
